package com.cowin.utils;

import com.cowin.sys.model.TmSysUser;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Copyright (C), 1998-2018, 奇瑞信息
 *
 * @author : 于超
 * @Date: 2019/9/18 11:31
 * @Description:
 */
public class ValidateUserUtils {

    public static boolean matches(TmSysUser user, String newPassword)
    {
        return user.getPassword().equals(shaEncryptText(newPassword));
    }


    private static  String shaEncryptText(String passwd) {
        MessageDigest md = null;
        byte[] bt = passwd.getBytes();
        String strDes = null;
        try {
            md = MessageDigest.getInstance("SHA-1");
            md.update(bt);
            // to HexString
            strDes = bytes2Hex(md.digest());
        } catch (NoSuchAlgorithmException e) {
            System.out.println("Invalid algorithm.");
            return null;
        }

        return strDes;
    }
    private static String bytes2Hex(byte[] bts) {
        String des = "";
        String tmp = null;
        for (int i = 0; i < bts.length; i++) {
            tmp = (Integer.toHexString(bts[i] & 0xFF));
            if (tmp.length() == 1) {
                des += "0";
            }
            des += tmp;
        }
        return des;
    }
}
